// MapPosition.cpp : Defines the entry point for the console application.
//

#include "StdAfx.h"
#include "MapPosition.h"
#include "Argument.h"
#include "ximage.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

void MakePos(int x, int y, int w, int h, int nx, int ny)
{
	for(int i=0; i<nx; i++)
	{
		for(int j=0; j<ny; j++)
		{
			printf("pos x%dy%d  %d %d %d %d\n", i, j, x+w*j, y+h*i, w, h);
		}
	}
}


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		CArgument arg(argc, argv);

		LPCTSTR szTodo = arg.get("-todo");

		if(szTodo == NULL)
		{
			printf("Usage: -todo <split | mkpos>\n\n"
				"\t\t split\n\t"
				"\t -input <input image file>\n\t"
				"\t -prefix <output image file prefix>\n\t"
				"\t -subfix <output image file subfix>\n\t"
				"\t\t mkpos\n\t"
				"\t -x  <x start>\n\t"
				"\t -y  <y start>\n\t"
				"\t -w  <w start>\n\t"
				"\t -h  <h start>\n\t"
				"\t -nx  <nx x unit count>\n\t"
				"\t -ny  <ny y unit count>\n\t"
				);
		}
		else
		{
			if(_tcsicmp(szTodo, "split") == 0)
			{
				LPCTSTR szInput = arg.get("-input");
				LPCTSTR szPrefix = arg.get("-prefix");
				LPCTSTR szSubfix = arg.get("-subfix");

				if(szSubfix == NULL)
				{
					szSubfix = _T(".png");
				}

				if(szInput && szPrefix && szSubfix)
				{
					CxImage Image;
					if(Image.Load(szInput))
					{
						Image.SplitPowerOf2(szPrefix, szSubfix);
					}
				}
			}
			else if(_tcsicmp(szTodo, "mkpos") == 0)
			{
				LPCTSTR sx = arg.get("-x");
				LPCTSTR sy = arg.get("-y");
				LPCTSTR sw = arg.get("-w");
				LPCTSTR sh = arg.get("-h");
				LPCTSTR snx = arg.get("-nx");
				LPCTSTR sny = arg.get("-ny");

				if(sx && sy && sw && sh && snx && sny)
				{
					int x = atoi(sx);
					int y = atoi(sy);
					int w = atoi(sw);
					int h = atoi(sh);
					int nx = atoi(snx);
					int ny = atoi(sny);

					MakePos(x, y, w, h, nx, ny);
				}
				else
				{
					printf("prameters lost\n");
				}
			}
		}

	}

	return nRetCode;
}

